/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */


package com.example.soscall.base;

import static com.jakewharton.rxbinding2.view.RxView.clicks;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.ecarx.xui.adaptapi.oncall.Call;
import com.example.soscall.BR;
import com.example.soscall.PhoneLiveData;
import com.example.soscall.utils.CallHelper;
import com.example.soscall.utils.RxUtils;
import com.example.soscall.utils.TimeUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


public abstract class BaseCallView<T extends ViewDataBinding> extends FrameLayout {
    private static String TAG = "CallView ";
    protected static Context mContext;
    protected T binding;
    protected boolean mStopTimeHandle = false;
    protected CompositeDisposable disposable;
    private Disposable timeIntervel;
    protected PhoneLiveData phoneLiveData;
    private int seconds;

    public BaseCallView(final Context context) {
        super(context);
        mContext = context;
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), getLayoutId(), this, true);
//        View view = LayoutInflater.from(context).inflate(getLayoutId(), this, true);
        disposable = new CompositeDisposable();
        phoneLiveData = CallHelper.getInstance().getPhoneLiveData();
        binding.setVariable(BR.viewmodel, phoneLiveData);
        phoneLiveData.getPhoneState().observeForever((state) ->{
            switch (state) {
                case Call.CALL_STATUS_OFFHOOK:
                    mStopTimeHandle = false;
                    int seconds;
                    if (CallHelper.getInstance().getCallDuration() > -1) {
                        seconds = CallHelper.getInstance().getCallDuration();
                    } else {
                        seconds = CallHelper.getInstance().getmCallSeconds() + 1;
                    }
                    phoneLiveData.setDuringSecond(TimeUtil.formatCallTime(seconds));
                    startTime();
                    break;
                case Call.CALL_STATUS_END:
                    stopTime();
                    break;
            }
        });

        initViews();

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    /**
     * 当为客服回拨后的界面时，隐藏挂断按钮。不是则显示
     */
    protected void updateViewStatus() {


    }



    public void stopTime() {
        mStopTimeHandle = true;
        if (timeIntervel != null) {
            timeIntervel.dispose();
            timeIntervel = null;
        }
    }

    private void startTime() {
        this.seconds = 0;
        if(timeIntervel!=null){
            return;
        }
        timeIntervel = RxUtils.intervel(1, TimeUnit.SECONDS, (result, data) -> {
            if (mStopTimeHandle) {
                return;
            }
            seconds++;
            if (this.phoneLiveData != null) {
//                if (CallHelper.getInstance().getCallDuration() > 0) {
//                   int seconds = CallHelper.getInstance().getCallDuration();
             phoneLiveData.setDuringSecond(TimeUtil.formatCallTime(seconds));
                if (seconds == 30) {
                    this.phoneLiveData.setPhoneState(Call.CALL_STATUS_END);

                }
//                setStateText(TimeUtil.formatCallTime(seconds));
            }
        });
    }


    /**
     * 当通话窗口和救援时间窗口同时出现时需要设置偏移,否则会覆盖
     *
     * @param params:       当前窗口LayoutParams
     * @param isRescueShow: 救援窗口是否显示 若显示则将通话窗口下移；若消失，则将通话窗口上移(位置还原)
     */
    public void setWindowOffset(WindowManager.LayoutParams params, boolean isRescueShow) {

    }

    /**
     * 设置当前窗口的参数: 比方位置
     */
    public void setWindowParams(WindowManager.LayoutParams params) {

    }


    public abstract int getLayoutId();

    public abstract void initViews();




    public void onClick(View view, Consumer consumer) {
        disposable.add(
                clicks(view).throttleFirst(1, TimeUnit.SECONDS).doAfterNext(o -> {
                    // TODO: 2022/8/19  埋点统计

                }).subscribe(consumer));
    }


}

